/* $RCSFile: SelTreeDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/SelTreeDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Helpers;

using Telerik.Web.UI;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// Implements a choice TreeView 
	/// To select values, move them from left tree to right one
	/// </summary>
	public class SelTreeDataType : BaseDataType
	{
		private string _dataValueField      =   string.Empty;
		private string _dataTextField       =   string.Empty;
		private string _dataFieldID         =   string.Empty;
		private string _dataFieldParentID   =   string.Empty;
        private string _NodeName            =   string.Empty;
        private int _MaxDataBindDepth       =   -1; //All
        private Unit _TreeHeight            =   new Unit("200px");

        
        public Unit TreeHeight
        {
            get
            { 
            return _TreeHeight;
            }
            set 
            { 
                _TreeHeight             =   value;
                if (_IsInitialized)
                {
                    SelTree selTree     =   (SelTree)EditControl;
                    selTree.TreeHeight  =   value;
                }
            }
        }
        
       public override Unit controlWidth
        {
            get
            {
                return base.controlWidth;
            }
            set
            {
                base.controlWidth       =   value;
                if (_IsInitialized)
                {
                    SelTree selTree     =   (SelTree)EditControl;
                    selTree.Width       =   value;
                }
            }
        }

         /// <summary>
		/// Constructor
		/// </summary>
		/// <param name="baseDirectory">Create if not exists</param>
		/// <param name="defaultDirectory">Create if not exists</param>
		public SelTreeDataType(string NodeName,object dataSource, string dataTextField, string dataValueField) : base()
		{
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine("[V]SelTreeDataType Ctor BEG.", DataTypeTraceSwitch.Sw.Info);
			InnerDataType	    =   PropertiesDataType.List;
			InnerDataSource     =   dataSource;
			DataValueField	    =   dataValueField;
			DataTextField	    =   dataTextField;
            _NodeName           =   NodeName;
            FullWidth           =   true; //Default
            controlWidth        =   new Unit("96%"); //Default
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine("[V]SelTreeDataType Ctor END.", DataTypeTraceSwitch.Sw.Info);
		}

		protected override void InitializeComponents()
		{
			if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]SelTreeDataType InitializeComponents BEG UseRadControl: {0}", _UsesRadControl),DataTypeTraceSwitch.Sw.Info);
			SelTree st          =   new SelTree();
            st.Width            =   controlWidth;
            st.TreeHeight       =   TreeHeight;
            st.Style.Add( HtmlTextWriterStyle.BorderStyle,"inset");
            st.Style.Add( HtmlTextWriterStyle.BorderWidth,"1");
            if ( ReadOnly )
                st.Enabled      =   false;
			innerControl        =   st;
            _IsInitialized      =   true;
		}
		
		public override string DataValueField
		{
			get
			{
				return _dataValueField;
			}
			set
			{
				_dataValueField = value;
			}
		}

		public override string DataTextField
		{
			get
			{
				return _dataTextField;
			}
			set
			{
				_dataTextField = value;
			}
		}
        
        public string DataFieldID
		{
			get
			{
                return _dataFieldID;
			}
			set
			{
                _dataFieldID    =   value;
			}
		}
        
        public string DataFieldParentID
		{
			get
			{
                return _dataFieldParentID;
			}
			set
			{
				_dataFieldParentID = value;
			}
		}
        
        public int MaxDataBindDepth
		{
			get
			{
                return _MaxDataBindDepth;
			}
			set
			{
				_MaxDataBindDepth = value;
			}
		}

		public override object DataSource
		{
			get
			{
				return InnerDataSource;
			}
			set
			{
				InnerDataSource = value;
			}
		}

   		public override void ResetDataSource(object newDataSrc,string SelVal)
		{
			if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]SelTreeDataType ResetDataSource : {0}, SelVal: {1}", newDataSrc,( SelVal == null )? string.Empty:SelVal),DataTypeTraceSwitch.Sw.Info);
			InnerDataSource = newDataSrc;
            if ( !string.IsNullOrEmpty(SelVal))
                    Val     =   SelVal;
            else    Val     =   string.Empty;
			if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]SelTreeDataType ResetDataSource innerControl null: {0}", innerControl == null ),DataTypeTraceSwitch.Sw.Info);
		}

        public override void DataBind()
        {
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]SelTreeDataType DataBind BEG.",DataTypeTraceSwitch.Sw.Info);
			try
			{
				SelTree selTree             =   (SelTree) EditControl;
                selTree.Width               =   controlWidth;
                selTree.DataTextField       =   DataTextField;
                selTree.DataValueField      =   DataValueField;
                selTree.DataFieldID         =   DataFieldID;
                selTree.DataFieldParentID   =   DataFieldParentID;
                selTree.MaxDataBindDepth    =   MaxDataBindDepth;
                selTree.SelectedTreeName    =   _NodeName;
                selTree.DataSource          =   DataSource;
                selTree.DataBind();
                selTree.SelectedValue       =   Val;
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]SelTreeDataType DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]SelTreeDataType DataBind END.",DataTypeTraceSwitch.Sw.Info);
        }

        public override string Val
		{
			get
			{
				return innerValue;
			}
			set
			{
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]SelTreeDataType Val SET BEG value: [{0}]", value),DataTypeTraceSwitch.Sw.Info);
				innerValue      = value;
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]SelTreeDataType Val SET innerControl is NULL: {0}", innerControl == null ),DataTypeTraceSwitch.Sw.Info);
                if ( innerControl == null )
                    return;
				//update value in control
   				DataBind();
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine("[V]SelTreeDataType Val SET END.",DataTypeTraceSwitch.Sw.Info);
			}
		}

        public override Control EditControl
		{
			get
			{
				if ( innerControl == null  )
					InitializeComponents();
				return innerControl;
			}
			set
			{
                if (DataTypeTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine("[V]SelTreeDataType EditControl SET BEG.", DataTypeTraceSwitch.Sw.Info);
				if (value is SelTree)
				{
					innerControl        =   value;
                    SelTree selTree     =   (SelTree)innerControl;
                    innerValue          =   selTree.SelectedValue;
                    if (DataTypeTraceSwitch.Sw.TraceVerbose)
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]SelTreeDataType EditControl SET END innerValue: {0}.",innerValue), DataTypeTraceSwitch.Sw.Info);
				return;
				}
				string str	=	string.Format("[E]SelTreeDataType EditControl Set: a SelTree value is required, a '{0}' is given.",value);
				if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine(str,DataTypeTraceSwitch.Sw.Info);
				throw new ArgumentException(str, "EditControl");
			}
		}

	}
}